<!--
 * @Date: 2022-04-24 14:44:48
 * @LastEditors: wuyuxin
 * @LastEditTime: 2022-04-27 10:02:38
 * @FilePath: \Vue3-ts-cms\src\components\page-search\src\page-search.vue
 * @description: 文件描述
-->
<template>
  <div class="page-search">
    <my-form v-bind="searchFormConfig" v-model="formData">
      <template #header>
        <h1 class="header">高级检索</h1>
      </template>
      <template #footer>
        <div class="handle-btns">
          <el-button @click="handleResetClick"
            ><el-icon><refresh /></el-icon>重置</el-button
          >
          <el-button @click="handleSearch" type="primary">
            <el-icon><search /></el-icon>搜索</el-button
          >
        </div>
      </template>
    </my-form>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref } from 'vue'
import MyForm from '@/base-ui/form'
export default defineComponent({
  props: {
    searchFormConfig: {
      type: Object,
      require: true
    }
  },
  components: {
    MyForm
  },
  emits: ['handleSearch', 'handleResetClick'],
  setup(props, { emit }) {
    console.log(props)
    const formItems = props.searchFormConfig?.formItems ?? []
    const formOriginData: any = {}
    for (const item of formItems) {
      formOriginData[item.prop] = ''
    }
    const formData = ref(formOriginData)

    // 2.优化二: 当用户点击重置
    const handleResetClick = () => {
      formData.value = formOriginData
      console.log('handleResetClick')
      emit('handleResetClick')
    }
    // 3.点击搜索传值
    const handleSearch = () => {
      emit('handleSearch', formData.value)
    }
    return {
      formData,
      handleResetClick,
      handleSearch
    }
  }
})
</script>

<style scoped>
.header {
  color: red;
}
.handle-btns {
  text-align: right;
  padding: 0 50px 20px 0;
}
</style>
